<template>
  <div class='container'>
    <div class="main">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="目录名称">
          <el-input v-model="formInline.user"></el-input>
        </el-form-item>
        <el-form-item label="状态" style="margin-left: 40px;">
          <el-select v-model="formInline.region" placeholder="请选择">
            <el-option label="启用" value="1"></el-option>
            <el-option label="禁用" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="eliminate">清除</el-button>
          <el-button type="primary" @click="inquire">查询</el-button>
        </el-form-item>
         <!-- 新增目录按钮 -->
         <el-button type="success" icon="el-icon-edit" style="float: right;" @click="newlyIncreased">新增目录</el-button>
        <!-- 返回学科 -->
        <el-button type="text" icon="el-icon-back"  v-if="this.$route.query.id" style="float: right; margin-right: 10px;" @click="$router.back()">返回学科</el-button>
      </el-form>
      <!-- 新增弹框 -->
      <el-dialog :title="this.farmData.creatorID ? '修改目录' : '添加目录'" :visible.sync="dialogVisible" width="400px" :before-close="handleClose">
        <el-form :model="farmData" :rules="rules" ref="ruleForm">
          <el-form-item label="所属学科" size="mini" prop="subjectID"  v-if="!this.$route.query.id">
            <el-select placeholder="请选择" style="width: 280px;" v-model="farmData.subjectID">
              <el-option v-for="item in pullDownList" :key="item.value" :value="item.value" :label="item.label"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="目录名称" size="mini" prop="directoryName">
            <el-input placeholder="请输入目录名称" style="width: 280px;" v-model="farmData.directoryName"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="handleClose">取 消</el-button>
          <el-button type="primary" @click="isOk">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 提示 -->
      <el-alert :closable="false" :title="'数据一共' + tableData.counts + '条'" type="info" show-icon style="margin-bottom: 15px;"></el-alert>
      <!-- 表格 -->
      <el-table :data="tableData.items" style="width: 100%;" :header-cell-style="{
        borderBottom: '2px solid #e8e8e8', backgroundColor: '#fafafa'
      }">
        <el-table-column type="index" label="序号" width="80">
        </el-table-column>
        <el-table-column prop="subjectName" label="所属学科">
        </el-table-column>
        <el-table-column prop="directoryName" label="目录名称">
        </el-table-column>
        <el-table-column prop="username" label="创建者">
        </el-table-column>
        <el-table-column prop="addDate" label="创建日期">
          <template v-slot="{ row }">
            {{ row.addDate.substring(0, 19).replace('T', ' ') }}
          </template>
        </el-table-column>
        <el-table-column prop="totals" label="面试题数量">
        </el-table-column>
        <el-table-column prop="state" label="状态">
          <template v-slot="{ row }">
            {{ row.state == 1 ? '已启用' : '已禁用' }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150">

          <template v-slot="{ row }">
            <el-button type="text" @click="condition(row)">{{ row.state == 1 ? '禁用' : '启用' }}</el-button>
            <el-button type="text" :disabled="row.state == 1" @click="upNwlyIncreased(row.id)">修改</el-button>
            <el-button type="text" :disabled="row.state == 1" @click="deleteMu(row.id)">删除</el-button>
          </template>

        </el-table-column>
      </el-table>
      <!-- 😒分页器 -->
      <div class="block">
        <span class="demonstration"></span>
        <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
          :current-page="currentPage4" :total="tableData.counts" layout="prev, pager, next, sizes, jumper"
          :page-sizes="[5, 10, 20, 50]" :page-size="pageData.pagesize">
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
// 获取学科接口
import { simple } from '@/api/hmmm/subjects'
import { list, changeState, add, update, detail, remove } from '@/api/hmmm/directorys'
export default {
  data () {
    return {
      formInline: {
        user: '',
        region: ''
      },
      currentPage4: 1,
      // 表格数据
      tableData: [],
      // 请求数据
      pageData: {
        page: 1,
        pagesize: 10
      },
      // 新增弹框
      dialogVisible: false,
      pullDownList: [],
      pullDownSelect: null,
      farmData: {
        subjectID: '',
        directoryName: ''
      },
      rules: {
        subjectID: [
          { required: true, message: '请选择所属学科', trigger: 'change' }
        ],
        directoryName: [
          { required: true, message: '请输入目录名称', trigger: 'blur' }
        ]
      }
    }
  },
  // 获取初始化数据
  created () {
    this.getList()
    // delete this.pageData.subjectID
  },
  methods: {
    // 获取初始化数据
    getList () {
      if (this.$route.query.id) {
        this.pageData.subjectID = this.$route.query.id
      } else {
        delete this.pageData.subjectID
      }
      list(this.pageData)
        .then(res => {
          this.tableData = res.data
        })
    },
    handleSizeChange (val) {
      // console.log(`每页 ${val} 条`)
      this.pageData.pagesize = val
      this.getList()
    },
    handleCurrentChange (val) {
      // console.log(`当前页: ${val}`)
      this.pageData.page = val
      this.getList()
    },
    // 查询\
    inquire () {
      if (this.formInline.user !== '' || this.formInline.region !== '') {
        this.pageData.directoryName = this.formInline.user
        this.pageData.state = this.formInline.region
      } else {
        delete this.pageData.directoryName
        delete this.pageData.state
      }
      this.getList()
    },
    // 清除
    eliminate () {
      this.formInline.user = ''
      this.formInline.region = ''
    },
    // 状态修改
    condition (row) {
      changeState({
        state: row.state === 1 ? 0 : 1,
        id: row.id
      }).then(() => {
        this.$message.success('操作成功')
        this.getList()
      })
    },
    // 添加按钮及修改按钮
    newlyIncreased () {
      this.dialogVisible = true
      simple({
        page: 1,
        pagesize: 100
      }).then(res => {
        this.pullDownList = res.data
      })
    },
    // 关闭弹框
    handleClose () {
      this.dialogVisible = false
      this.$refs.ruleForm.resetFields()
      this.farmData = {
        subjectID: '',
        directoryName: ''
      }
    },
    // 提交修改和新增
    isOk () {
      const fn = this.farmData.creatorID ? update : add
      this.$refs.ruleForm.validate()
        .then(() => {
          fn(this.farmData).then(() => {
            this.$message.success('操作成功')
            this.handleClose()
            this.getList()
          })
        })
    },
    // 修改
    upNwlyIncreased (id) {
      this.dialogVisible = true
      detail({
        id: id
      }).then(res => {
        this.farmData = res.data
        simple({
          page: 1,
          pagesize: 100
        }).then(res => {
          this.pullDownList = res.data
        })
      })
    },
    // 删除目录
    deleteMu (id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        remove({
          id: id
        }).then(() => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getList()
        })
      })
    }
  }
}
</script>

<style scoped lang='scss'>
.container {
  padding: 0 10px;
  margin: 10px 0;
}

.main {
  padding: 20px;
  background-color: #fff;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  border: 1px solid #ebeef5;
  border-radius: 5px;
  color: #303133;
}

.demo-form-inline {
  margin-left: 10px;
}

.block {
  margin-top: 20px;
  text-align: right;
}
</style>
